package com.cyj.crm02.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyj.crm02.entity.Department;
import com.cyj.crm02.entity.Permission;
import com.cyj.crm02.entity.Role;
import com.cyj.crm02.service.PermissionService;
import com.cyj.crm02.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/list")
    public List<Permission> roles(){
        return permissionService.list();
    }

    /**
     *  查询一页权限信息
     *
     */
    @GetMapping("/pageList")
    public Map getList(Integer page, Integer limit ){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Permission> page1 = new Page<Permission>();
        page1.setSize(limit);
        page1.setCurrent(page);

        IPage<Permission> iPage = permissionService.selectPermissionList(page1);

        map.put("msg","查询情况");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);

        System.out.println(map);
        return map;
    }

    /**
     *  删除权限
     */
    @DeleteMapping("/del/{permissionId}")
    public Map del(@PathVariable Integer permissionId){
        Permission permission = new Permission();
        permission.setPermissionId(permissionId);
        permission.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        int num = 0;
        boolean flag = permissionService.updateById(permission);
        if (flag==true){
            num = 1;
            result.put("msg","删除成功");
        }else {
            result.put("msg","删除失败");
        }
        result.put("state",num);
        return result;
    }

    /**
     *  新增权限
     */
    @PostMapping(value="/add",produces = "application/json;charset=UTF-8")
    public Map add(Permission permission){
        permission.setCreateTime(StringUtils.getNowTime());
        permission.setUpdateTime(StringUtils.getNowTime());
        System.out.println("添加的参数permission"+permission);
        Map<String,Object> result = new HashMap<String,Object>();
        int num = 0;

        boolean flag = permissionService.save(permission);
        if(flag==true){
            num = 1;
        }

        result.put("state",num);
        if(num ==1){
            result.put("msg","添加成功");
        }else {
            result.put("msg","添加失败");
        }
        return result;
    }

    /**
     *  修改权限
     */
    @PutMapping("/update")
    public Map edit(Permission permission){
        permission.setUpdateTime(StringUtils.getNowTime());
        Map<String,Object> result = new HashMap<String,Object>();
        boolean flag = permissionService.updateById(permission);
        System.out.println(flag);
        int num = 0;
        if (flag == true){
            num = 1;
        }

        result.put("state",num);
        return result;
    }
}
